import { createRouter, createWebHistory } from 'vue-router'
import { usePlaygroundStore } from '../stores/playground'
import { useExamplesStore } from '../stores/examples'

const routes = [
    {
        path: '/',
        name: 'Home',
        component: () => import('../views/HomePage.vue')
    },
    {
        path: '/playground/:id',
        name: 'Playground',
        component: () => import('../views/PlaygroundPage.vue'),
        beforeEnter: async (to, from, next) => {
            const playgroundStore = usePlaygroundStore()
            const examplesStore = useExamplesStore()

            // 重置状态
            playgroundStore.resetState()

            // 检查示例是否存在
            const example = examplesStore.getExampleById(to.params.id)
            if (!example) {
                next({ name: 'Home' })
                return
            }

            next()
        }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

export default router